﻿import urllib2
#import urllib
#import httplib
import cookielib
#import json
#import hashlib
#import xmpp
import random
#import android
import appuifw
import e32
import btsocket

old_title= appuifw.app.title
appuifw.app.title = u'新浪微博'

textctl=appuifw.Text()

alock=e32.Ao_lock()

def setnet():
	apid=btsocket.select_access_point()
#	f=open('e:\\miniblogapid.txt','w')
#	f.write(repr(apid))
#	f.close()
	apo=btsocket.access_point(apid)
	btsocket.set_default_access_point(apo)

def send():
	#droid = android.Android()
	#cookie

	#setnet()

	cj = cookielib.CookieJar()
	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
	urllib2.install_opener(opener)

	yoursinaaccount='test';
	yourpassworldmd5='xxxxxxxxxxxxxxxxxxx'
	#login
	resp=urllib2.urlopen('http://login.sina.com.cn/sso/login.php?username='+yoursinaaccount+'&password='+yourpassworldmd5+'&pwencode=md5&returntype=TEXT2')
	#jsonres=json.loads(resp.read())

	#print jsonres

	#send text
	#input=droid.getInput('sina_miniblog','please input your msg')
	#content = "%(result)s" % input
	msg=textctl.get()
	data='content='+msg.encode('utf-8')+'&pic=&styleid=1'

	req=urllib2.Request('http://t.sina.com.cn/mblog/publish.php?rnd='+str(random.random()),data)
	#req=urllib2.urlopen('http://t.sina.com.cn/mblog/publish.php?rnd=0.05929241907943594',data)
	req.add_header('Referer','http://t.sina.com.cn/mymblog.php')
	resp=urllib2.urlopen(req)
	
	appuifw.note(u'发送新浪微博成功！')
	textctl.set(u'')
	#alock.signal()
	#print resp.read()

def exit_key_handler_func():
	alock.signal()

appuifw.app.exit_key_handler = exit_key_handler_func

appuifw.app.body = textctl
appuifw.app.menu = [(u'发微博', send),(u'退出',exit_key_handler_func)]

appuifw.app.screen='normal'
textctl.focus = True
alock.wait()

appuifw.app.exit_key_handler = None
appuifw.app.title=old_title
#appuifw.app.menu=None
